package com.example.forumsystem.controller;


import com.example.forumsystem.common.AppResult;
import com.example.forumsystem.common.ResultCode;
import com.example.forumsystem.model.Board;
import com.example.forumsystem.services.IBoardService;
import com.example.forumsystem.services.impl.BoardServiceImpi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "板块相关接口")
@RequestMapping("/board")
@RestController
public class BoradController {
    /**
     * 获取板块列表
     */
    @Resource
    private IBoardService boardService;
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;
    @GetMapping("/boardList")
    @ApiOperation("获取板块列表")
    public AppResult<List<Board>> boardList() {


        // 获取板块列表
        List<Board> boards = boardService.selectByNum(indexBoardNum);


        // 判断是否为空.如果为空给一个默认的链表
        if (boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }
    @ApiOperation("获取板块信息")
    @GetMapping("/selectById")
    public AppResult selectById(Long id) {

        // 参数校验
        if(id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 调用service查询板块
        Board board = boardService.selectById(id);
        // 返回结果
        return AppResult.success(board);
    }
}
